<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" width="80%" destroy-on-close @closed="$emit('closed')" align-center :close-on-click-modal="false">
        <el-container>
            <el-main>
                <el-form :model="form" :rules="rules" :disabled="mode == 'show'" ref="dialogForm" label-width="110px" label-position="right" style="height:650px;overflow:auto">
                    <el-row>
                        <el-col :span="6">
                            <el-form-item label="合同号" prop="contract_no">
                                <el-input v-model="form.contract_no" placeholder="请输入合同号" readonly></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item label="工厂货号" prop="factory_number" >
                                <el-input v-model="form.factory_number" placeholder="请出入工厂货号" readonly></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item label="批次号" prop="batch_number">
                                <el-input v-model="form.batch_number" placeholder="请输入批次号" readonly></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item label="后整组" prop="finish_group_id">
                                <el-select v-model="form.finish_group_id" clearable style="width: 100%" placeholder="请选择">
                                    <el-option v-for="item in finishData" :key="item.id" :label="item.title" :value="item.id" />
                                </el-select>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="6">
                            <el-form-item label="客户名称" prop="custom_name">
                                <el-input v-model="form.custom_name" readonly></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item label="生产工序" prop="produce_process">
                                <el-select v-model="form.produce_process" clearable style="width: 100%" placeholder="请选择" @change="proProcess" multiple>
                                    <el-option v-for="item in proData" :key="item.id" :label="item.title" :value="item.id" />
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item label="承制人" prop="supplier_id">
                                <el-select v-model="form.supplier_id" clearable style="width: 100%" placeholder="请选择" @change="suppChange" filterable>
                                    <el-option v-for="item in SupplierList" :key="item.apply_id + '_' + item.price" :label="item.referred_as" :value="item.apply_id + '_' + item.price" />
                                </el-select>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="6">
                            <el-form-item label="客户订单号" prop="order_no" >
                                <el-input v-model="form.order_no" placeholder="请输入客户订单号" readonly></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item label="单价" prop="price" >
                                <el-input v-model="form.price" placeholder="请输入单价" ></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item label="单位" prop="unit">
                                <el-select v-model="form.unit" clearable style="width: 100%" placeholder="请选择" @change="proProcess">
                                    <el-option label="元/打" :value="1" />
                                    <el-option label="元/件" :value="2" />
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item label="针种" prop="needle_seed">
                                <el-select v-model="form.needle_seed" clearable style="width: 100%" filterable placeholder="请选择" multiple>
                                    <el-option v-for="item in needleList" :key="item.id" :label="item.title" :value="item.id" />
                                </el-select>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="6">
                             <el-form-item label="要求完成日期" prop="due_date" >
                                <el-date-picker v-model="form.due_date" :value-format="'YYYY-MM-DD'" style="width:100%" type="date" placeholder="请选择要求完成时间" :disabled-date="disabledDate" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item label="要求首次交期" prop="first_delivery_date" >
                                <el-date-picker v-model="form.first_delivery_date" :value-format="'YYYY-MM-DD'" style="width:100%" type="date" placeholder="请选择要求首次交间" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item label="备注" prop="remark">
                                <el-input v-model="form.remark" placeholder="请输入备注"></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <!-- 订单明细 -->
                    <el-col :span="24">
                        <el-form-item label="订单明细" prop="size_data">
                            <sc-form-table ref="table" v-model="form.size_data" :addTemplate="addTemplate" placeholder="暂无数据" hideAdd hideDelete>
                                <el-table-column prop="size" label="尺码" width="120" align="center"></el-table-column>
                                <el-table-column prop="produce_num" label="生产单数量" width="140" align="center"></el-table-column>
                                <el-table-column prop="pending_num" label="待后整数量" width="180" align="center"></el-table-column>
                                <el-table-column prop="weaving_num" label="后整数量" width="180" align="center">
                                    <template #default="scope">
                                        <el-input v-model="scope.row.weaving_num" placeholder="请输入后整数量"></el-input>
                                    </template>
                                </el-table-column>
                            </sc-form-table>
                        </el-form-item>
                    </el-col>
                </el-form>
            </el-main>
            <el-footer>
                <div style="display:flex;justify-content: end;">
                    <el-button @click="visible=false" >取消</el-button>
                    <el-button type="primary" :loading="isSaveing" @click="submit(1)">保存</el-button>
                    <el-button :loading="isSaveing" type="primary" @click="submit(2)">保存并打印</el-button>
                </div>
            </el-footer>
        </el-container>
	</el-dialog>
</template>

<script>
	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '新增委外合同单',
					edit: '修改生产任务',
					show: '查看'
				},
                dialog: {
				    save: false,
			    },
				visible: false,
				isSaveing: false,
                grade:"潜力",
				//表单数据
				form: {
					id:"",
                    contract_no:"",
					produce_no: "",
                    order_no: "",
                    batch_number:"",
                    price: "",
                    unit: "",
                    supplier_id:"",
                    produce_process:"",
                    needle_seed:"",
                    due_date:this.getNextDay(),
                    first_delivery_date:"",
                    remark:"",
                    size_data:[],   //后整信息
                    process_id:8,
                    finish_group_id:"",
				},
                size:[],
				//验证规则
				rules: {
                    produce_process: [
                        {required: true, message: '请选择生产工序'}
                    ],
                    supplier_id: [
                        {required: true, message: '请选择承制人'}
                    ],
                    price: [
                        {required: true, message: '请输入单价'}
                    ],
                    unit: [
                        {required: true, message: '请输入单位'}
                    ],
                    due_date: [
                        {required: true, message: '请选择日期'}
                    ],
				},
				goodsList: [],  //商品款式
                curList:[],//客户
                needleList:[],//针种
                woolList:[],//毛种
                humList:[],//员工
                SupplierList:[],//承制人
                proData : [],
                finishData:[],//后整组
                supplier_id: "",
			}
		},
		mounted() {
            this.getCustomerList()
            this.getNeedle()
            this.getHum()
            this.getFinish()
		},
		methods: {
            getNextDay() {
                const today = new Date();
                today.setDate(today.getDate() + 1); // 获取明天的日期
                return today.toISOString().split('T')[0]; // 转换为 'YYYY-MM-DD' 格式
            },
            disabledDate(time) {
                return time.getTime() < Date.now() - 8.64e7; // 8.64e7 是一天的毫秒数
            },
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this
			},
			//表单提交方法
			submit(e){
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaveing = true;
						var res  = [];
						res = await this.$API.produce.contract.createContract.post(this.form);
                        if(res.code == 1){
                            if(e==1){
                                this.visible = false;
                                this.$emit('success', this.form, this.mode)
                                this.$message.success("操作成功")
                            }else{
                                var reqData = { contract_no_arr: res.data.contract_no_arr};
                                var print = await this.$API.print.contractPrint.get(reqData);
                                if (print.code == 1) {
                                    this.$emit('success', this.form, this.mode,print.data)
                                    this.visible = false;
                                } else {
                                    this.$alert(print.message, "提示", { type: "error" });
                                    this.isSaveing = false;
                                }
                            }
                        }else{
                            this.$alert(res.message, "提示", {type: 'error'})
                            this.isSaveing = false;
                        }
					}else{
						return false;
					}
				})
			},
			//表单注入数据
			async setData(data){
                this.form.id = data.id
                var res = await this.$API.produce.getProduce.detail.get({id:data.id,process_id:8,});
                this.form.contract_no = res.data.contract_no
                this.form.factory_number = res.data.factory_number
                this.form.produce_no = res.data.produce_no
                this.form.order_no = res.data.order_no
                this.form.custom_name = res.data.custom_name
                this.form.batch_number = res.data.batch_number
                this.form.needle_seed = res.data.needle_seed
                this.form.remark = res.data.remark
                let orderDetail = []
                res.data.orderDetail.map(item=>{
                    let detail = {
                        size:item.goods_size,
                        produce_num:item.produce_num,
                        pending_num:item.pending_num,
                        weaving_num:item.pending_num,
                    }
                    orderDetail.push(detail)
                })
                this.form.size_data = orderDetail
                var proDate = await this.$API.produce.waitSettled.getProcessOfSubProcess.get({produce_no:res.data.produce_no,process_id:8,});
                this.proData = proDate.data
                let proProcess = []
                proDate.data.map(item=>{
                    proProcess.push(item.id)
                })
                this.form.produce_process = proProcess
                this.proProcess(this.form.produce_process)
			},
            // 获取客户
            async getCustomerList(){
				var res = await this.$API.all.customer.get();
				this.curList = res.data;
			},
            //所有针种
            async getNeedle(){
                var res = await this.$API.all.needle.get();
                this.needleList = res.data;
            },
            // 跟单人
            async getHum(){
                var res = await this.$API.all.staff.get();
                this.humList = res.data;
            },
            async getFinish(){
                var res = await this.$API.all.finish.get();
                this.finishData = res.data;
            },
            // 切换生产工序,获取承制人
            async proProcess(e){
                var res = await this.$API.produce.contract.getSupplierList.get({produce_process:e,factory_number:this.form.factory_number});
                let a = []
                res.data.map(item=>{
                    let unit = item.unit==1?"元/打":"元/件"
                    a.push({
                        referred_as:item.price!="" ?item.referred_as + "+" + item.price + unit : item.referred_as,
                        apply_id:item.apply_id,
                        price:item.price,
                        unit:item.unit
                    })
                })
                this.SupplierList = a
            },
            suppChange(e){
                let price = e.split("_")
                this.form.price = price[1]
                this.form.supplier_id = price[0]
                console.log('price :>> ', price);
                this.SupplierList.map(item=>{
                    if(price[0] == item.apply_id){
                        this.form.unit = item.unit
                    }
                })
            },
		}
	}
</script>

<style lang="scss"></style>
